package org.jeecg.modules.rooms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.rooms.entity.CesHouseLongRentCharge;
import org.jeecg.modules.rooms.entity.CesHouseLongRentScheme;
import org.jeecg.modules.rooms.service.ICesHouseLongRentChargeService;
import org.jeecg.modules.rooms.service.ICesHouseLongRentSchemeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/rooms/cesHouseLongRentScheme")
@Api(tags = "ces_hour_room_rule")
@Slf4j
public class CesHouseLongRentSchemeController extends JeecgController<CesHouseLongRentScheme, ICesHouseLongRentSchemeService> {

    @Resource
    private ICesHouseLongRentChargeService houseLongRentChargeService;

    @AutoLog(value = "新增房价方案")
    @ApiOperation(value="新增房价方案", notes="新增房价方案")
    @PostMapping(value = "/add")
    public Result<Boolean> add(@RequestBody CesHouseLongRentScheme scheme){
        boolean isSuccess = service.save(scheme);
        if (isSuccess && CollUtil.isNotEmpty(scheme.getHouseLongRentChargeList())){
            scheme.getHouseLongRentChargeList().forEach(e -> e.setSchemeId(scheme.getId()));
            isSuccess = houseLongRentChargeService.saveBatch(scheme.getHouseLongRentChargeList());
        }
        return isSuccess ? Result.ok("新增成功") : Result.error("新增失败");
    }

    @AutoLog(value = "修改房价方案")
    @ApiOperation(value="修改房价方案", notes="修改房价方案")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/edit")
    public Result<Boolean> edit(@RequestBody CesHouseLongRentScheme scheme){
        if (StrUtil.isEmpty(scheme.getId())){
            Result.error("未找到要修改的方案");
        }
        service.updateById(scheme);
        houseLongRentChargeService.remove(Wrappers.<CesHouseLongRentCharge>lambdaQuery().eq(CesHouseLongRentCharge::getSchemeId,scheme.getId()));
        if (CollUtil.isNotEmpty(scheme.getHouseLongRentChargeList())){
            scheme.getHouseLongRentChargeList().forEach(e -> e.setSchemeId(scheme.getId()));
            houseLongRentChargeService.saveBatch(scheme.getHouseLongRentChargeList());
        }
        return Result.ok("修改成功");
    }

    @AutoLog(value = "查询房价方案")
    @ApiOperation(value="查询房价方案", notes="查询房价方案")
    @GetMapping(value = "/page")
    public Result<Page<CesHouseLongRentScheme>> page(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                     String hotelId){
        Page<CesHouseLongRentScheme> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<CesHouseLongRentScheme> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CesHouseLongRentScheme::getHotelId, hotelId);
        Page<CesHouseLongRentScheme> houseLongRentSchemePage = service.page(page, queryWrapper);
        houseLongRentSchemePage.getRecords().forEach(e -> {
            List<CesHouseLongRentCharge> list = houseLongRentChargeService.list(Wrappers.<CesHouseLongRentCharge>lambdaQuery()
                    .eq(CesHouseLongRentCharge::getSchemeId, e.getId()).isNull(CesHouseLongRentCharge::getLivingOrderId));
            e.setHouseLongRentChargeList(list);
        });
        return Result.ok(houseLongRentSchemePage);
    }
}
